#ifndef __UDP_SERVER_HPP__
#define __UDP_SERVER_HPP__
#include "Common.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <string>
#include <errno.h>
#include <memory>
#include "Log.hpp"
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <functional>
#include "InetAddr.hpp"
#include "ThreadPool.hpp"
using namespace LogModule;
using namespace ThreadPoolModule;
using route_t = std::function<void(int sockfd, const std::string &message)>;
using task_t = std::function<void()>;
using adduser_t = std::function<void(InetAddr &id)>;
using func_t = std::function<std::string(std::string)>;
using remove_t = std::function<void(InetAddr &id)>;
const static int defaultsockfd = -1;
const static std::string defaultip = "127.0.0.1"; // 表示本地主机，用于代码测试，
const static uint16_t defaulteport = 8080;
class nocopy
{
public:
    nocopy() {}
    nocopy(const nocopy &) = delete;
    const nocopy &operator=(const nocopy &) = delete;
    ~nocopy() {}
};
class UdpServer : public nocopy
{

public:
    UdpServer(func_t func, const uint16_t port = defaulteport)
        : _sockfd(defaultsockfd), _isrunning(0), _addr(port), _func(func)
    {
    }
    void InitServer()
    {
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket" << strerror(errno);
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket success 值为" << _sockfd;

        int n = ::bind(_sockfd, _addr.NetAddr(), _addr.NetAddrLen());
        if (n < 0)
        {
            LOG(LogLevel::FATAL) << "bind:" << strerror(errno);
            Die(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success";
    }
    void RegisterService(adduser_t adduser, route_t route, remove_t removeuser)
    {
        _adduser = adduser, _route = route, _removeuser = removeuser;
    }
    void Start()
    {
        if (_isrunning)
            return;
        _isrunning = true;
        char buff[1024];
        sockaddr_in peer;
        socklen_t len = sizeof peer;
        while (true)
        {
            ssize_t n = recvfrom(_sockfd, buff, sizeof(buff) - 1, 0, CONV(&peer), &len);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "recvfrom fail" << strerror(errno);
                Die(3);
            }
            if (n >= 0)
            {
                buff[n] = 0;
                InetAddr cli(peer);
                string message;
                if (strcmp(buff, "QuitClient") == 0)
                {
                    _removeuser(cli);
                    message = cli.Addr() + "#" + "我走了朋友们，你们聊";
                    task_t r = std::bind(UdpServer::_route, _sockfd, message);
                }
                // 插入用户
                else
                {
                    _adduser(cli);
                    message = cli.Addr() + "#" + buff;
                }
                task_t r = std::bind(UdpServer::_route, _sockfd, message);
                ThreadPool<task_t>::getinstance()->Equeue(r);
                std::string clientinfo = cli.Ip() + ":" + std::to_string(cli.Port()) + "发送了以下消息： " + buff;
                // 显示客户端发了什么消息
                LOG(LogLevel::DEBUG) << clientinfo;

                if (n < 0)
                {
                    LOG(LogLevel::FATAL) << "sendto fail" << strerror(errno);
                }
            }
        }
        _isrunning = false;
    }
    void End() {}
    ~UdpServer()
    {
        if (_sockfd > defaultsockfd)
        {
            close(_sockfd);
        }
    }

private:
    int _sockfd;
    InetAddr _addr;
    bool _isrunning; // 服务器是否启动
    func_t _func;
    adduser_t _adduser;
    route_t _route;
    remove_t _removeuser;
};

#endif